##############################################################################################################
################################### Appendix (B) #############################################################
##############################################################################################################

#Load packages and import the shape file
require(sf)
require(maptools)
require(rgeos)
require(plyr)
library(RColorBrewer)
library(moranfast)
shape <- read_sf(dsn = "egy_admbnda_adm2_capmas_20170421.shp")


#### Prepare the data by merging the spatial data

#The next set of steps match the violence data with the spatial variables. However, this requires some adjustments to naming of districts 
# to align the units together (due to different naming conventions between the violence electoral data and the spatial files)

##Import a file where districts' naming is aligned between the shape files and electoral data
namedata <- read.csv("match_names.csv")
names(namedata)[3] <- "ADM2_PCODE"

##Merge this name file with the shape file 
shape2 <- merge(shape, namedata, by=c("ADM2_PCODE"))

#This will collapse the geometries into the districts (including some districts that has no electoral relevance at the time of the election)
shape2_union <- shape2 %>%
  group_by(district_name) %>% 
  summarize(geometry = st_combine(geometry))


#Import the violence data and adjust naming according to the spatial units
data <- read_dta("data_2rounds.dta")

data$district_name[data$district_name=="El-Mahad El-Fany"] <- "Shobra"
data$district_name[data$district_name=="El-Manyal"] <- "Masr El-Qadimah"
data$district_name[data$district_name=="Ghorbal"] <- "Moharam Bek"
data$district_name[data$district_name=="Basandeela"] <- "Belkass"
data$district_name[data$district_name=="Atmeeda"] <- "Meet Ghamr"
data$district_name[data$district_name=="Al-Teleen"] <- "Menia Al-Qamh"
data$district_name[data$district_name=="Ashkor"] <- "Faqous"
data$district_name[data$district_name=="Al-Agouzein"] <- "Disouq"
data$district_name[data$district_name=="Berma"] <- "Tanta"
data$district_name[data$district_name=="Mahala Rouh"] <- "Tanta"
data$district_name[data$district_name=="Beshbeesh"] <- "Markaz Shurta Mahalla al-Kubra"
data$district_name[data$district_name=="Nehtai"] <- "Zefta"
data$district_name[data$district_name=="Batanoun"] <- "Shebeen Al-Koum"
data$district_name[data$district_name=="Istenha"] <- "El-Bagour"
data$district_name[data$district_name=="Shamma"] <- "Ashmoun"
data$district_name[data$district_name=="Mazghouna"] <- "Badrasheen"
data$district_name[data$district_name=="Al-Dowayrat"] <- "Al-Monshaah"
data$district_name[data$district_name=="Al-Ra'eesiya"] <- "Naga' Hamadi"
data$district_name[data$district_name=="Al-Agamiyeen"] <- "Ibshouwai"



############################ Moran's Test for First Round 

#Subset the data for round 1
subdata<- subset(data, select=c("district_name", "allintimid", "voter_total", 
                                "state_intimidation", "nonstate_intimidation"), data$round==1)


#We will collapse the data by district (this is due to changing names, which could create duplicates)
data2rounds <- aggregate(cbind(allintimid, voter_total, state_intimidation, nonstate_intimidation) ~ district_name, subdata, FUN=sum)

#Incorporate the spatial data
shape2_union <- shape2 %>%
  group_by(district_name) %>% 
  summarize(geometry = st_combine(geometry))

shape2_union <- merge(shape2_union, data2rounds, by=c("district_name"))

cord <- as.matrix(coordinates(as_Spatial(shape2_union$geometry)))
cord <- cbind(cord, as.vector(shape2_union$district_name))
cord <- as.data.frame(cord)
names(cord)[1] <- "lat"
names(cord)[2] <- "lon"
names(cord)[3] <- "district_name"
cdata <- merge(data2rounds, cord, by="district_name", all=T)

cdata$lat <- as.numeric(cdata$lat)
cdata$lon <- as.numeric(cdata$lon)
cdata<- cdata[complete.cases(cdata),]

#Run Moran's test
moranfast(x=cdata$allintimid, c1=cdata$lon, c2=cdata$lat, alternative="two.sided")


############################ Moran's Test for Second Round 

#Subset the data for round 2
subdata<- subset(data, select=c("district_name", "allintimid", "voter_total", 
                                "state_intimidation", "nonstate_intimidation"), data$round==2)


#We will collapse the data by district (this is due to changing names, which could create duplicates)
data2rounds <- aggregate(cbind(allintimid, voter_total, state_intimidation, nonstate_intimidation) ~ district_name, subdata, FUN=sum)

#Incorporate the spatial data
shape2_union <- shape2 %>%
  group_by(district_name) %>% 
  summarize(geometry = st_combine(geometry))

shape2_union <- merge(shape2_union, data2rounds, by=c("district_name"))

cord <- as.matrix(coordinates(as_Spatial(shape2_union$geometry)))
cord <- cbind(cord, as.vector(shape2_union$district_name))
cord <- as.data.frame(cord)
names(cord)[1] <- "lat"
names(cord)[2] <- "lon"
names(cord)[3] <- "district_name"
cdata <- merge(data2rounds, cord, by="district_name", all=T)

cdata$lat <- as.numeric(cdata$lat)
cdata$lon <- as.numeric(cdata$lon)
cdata<- cdata[complete.cases(cdata),]

#Run Moran's test
moranfast(x=cdata$allintimid, c1=cdata$lon, c2=cdata$lat, alternative="two.sided")

############################ Moran's Test for Two Rounds Aggregated 

#Subset the data for main variables
subdata<- subset(data, select=c("district_name", "allintimid", "voter_total", 
                                "state_intimidation", "nonstate_intimidation"))


#We will collapse the data by district 
data2rounds <- aggregate(cbind(allintimid, voter_total, state_intimidation, nonstate_intimidation) ~ district_name, subdata, FUN=sum)

#Incorporate the spatial data
shape2_union <- shape2 %>%
  group_by(district_name) %>% 
  summarize(geometry = st_combine(geometry))

shape2_union <- merge(shape2_union, data2rounds, by=c("district_name"))

cord <- as.matrix(coordinates(as_Spatial(shape2_union$geometry)))
cord <- cbind(cord, as.vector(shape2_union$district_name))
cord <- as.data.frame(cord)
names(cord)[1] <- "lat"
names(cord)[2] <- "lon"
names(cord)[3] <- "district_name"
cdata <- merge(data2rounds, cord, by="district_name", all=T)

cdata$lat <- as.numeric(cdata$lat)
cdata$lon <- as.numeric(cdata$lon)
cdata<- cdata[complete.cases(cdata),]

#Run Moran's test
moranfast(x=cdata$allintimid, c1=cdata$lon, c2=cdata$lat, alternative="two.sided")


############################ Moran's Test for Phase (1) 

#Subset the data for main variables
subdata<- subset(data, select=c("district_name", "allintimid", "voter_total", 
                                "state_intimidation", "nonstate_intimidation"), data$phase==1)


#We will collapse the data by district 
data2rounds <- aggregate(cbind(allintimid, voter_total, state_intimidation, nonstate_intimidation) ~ district_name, subdata, FUN=sum)

#Incorporate the spatial data
shape2_union <- shape2 %>%
  group_by(district_name) %>% 
  summarize(geometry = st_combine(geometry))

shape2_union <- merge(shape2_union, data2rounds, by=c("district_name"))

cord <- as.matrix(coordinates(as_Spatial(shape2_union$geometry)))
cord <- cbind(cord, as.vector(shape2_union$district_name))
cord <- as.data.frame(cord)
names(cord)[1] <- "lat"
names(cord)[2] <- "lon"
names(cord)[3] <- "district_name"
cdata <- merge(data2rounds, cord, by="district_name", all=T)

cdata$lat <- as.numeric(cdata$lat)
cdata$lon <- as.numeric(cdata$lon)
cdata<- cdata[complete.cases(cdata),]

#Run Moran's test
moranfast(x=cdata$allintimid, c1=cdata$lon, c2=cdata$lat, alternative="two.sided")



############################ Moran's Test for Phase (2) 

#Subset the data for main variables
subdata<- subset(data, select=c("district_name", "allintimid", "voter_total", 
                                "state_intimidation", "nonstate_intimidation"), data$phase==2)


#We will collapse the data by district 
data2rounds <- aggregate(cbind(allintimid, voter_total, state_intimidation, nonstate_intimidation) ~ district_name, subdata, FUN=sum)

#Incorporate the spatial data
shape2_union <- shape2 %>%
  group_by(district_name) %>% 
  summarize(geometry = st_combine(geometry))

shape2_union <- merge(shape2_union, data2rounds, by=c("district_name"))

cord <- as.matrix(coordinates(as_Spatial(shape2_union$geometry)))
cord <- cbind(cord, as.vector(shape2_union$district_name))
cord <- as.data.frame(cord)
names(cord)[1] <- "lat"
names(cord)[2] <- "lon"
names(cord)[3] <- "district_name"
cdata <- merge(data2rounds, cord, by="district_name", all=T)

cdata$lat <- as.numeric(cdata$lat)
cdata$lon <- as.numeric(cdata$lon)
cdata<- cdata[complete.cases(cdata),]

#Run Moran's test
moranfast(x=cdata$allintimid, c1=cdata$lon, c2=cdata$lat, alternative="two.sided")


############################ Moran's Test for Phase (3) 

#Subset the data for main variables
subdata<- subset(data, select=c("district_name", "allintimid", "voter_total", 
                                "state_intimidation", "nonstate_intimidation"), data$phase==3)


#We will collapse the data by district 
data2rounds <- aggregate(cbind(allintimid, voter_total, state_intimidation, nonstate_intimidation) ~ district_name, subdata, FUN=sum)

#Incorporate the spatial data
shape2_union <- shape2 %>%
  group_by(district_name) %>% 
  summarize(geometry = st_combine(geometry))

shape2_union <- merge(shape2_union, data2rounds, by=c("district_name"))

cord <- as.matrix(coordinates(as_Spatial(shape2_union$geometry)))
cord <- cbind(cord, as.vector(shape2_union$district_name))
cord <- as.data.frame(cord)
names(cord)[1] <- "lat"
names(cord)[2] <- "lon"
names(cord)[3] <- "district_name"
cdata <- merge(data2rounds, cord, by="district_name", all=T)

cdata$lat <- as.numeric(cdata$lat)
cdata$lon <- as.numeric(cdata$lon)
cdata<- cdata[complete.cases(cdata),]

#Run Moran's test
moranfast(x=cdata$allintimid, c1=cdata$lon, c2=cdata$lat, alternative="two.sided")


